package remato.domain.common.impl;

import java.util.HashSet;
import java.util.Set;

import remato.common.domain.impl.base.DomainName;
import remato.domain.common.Status;

public class StatusImpl extends DomainName implements Status {
	private Set<Status> nextStatuses = new HashSet<Status>();
	
	public StatusImpl() {
		this(null,null);
	}

	public StatusImpl(String name) {
		this(name,null);
	}
	
	public StatusImpl(String name, String description) {
		setName(name);
		setDescription(description);
	}
	
	public Set<Status> getNextStatuses() {
		return nextStatuses;
	}

	public void setNextStatuses(Set<Status> nextStatuses) {
		this.nextStatuses = nextStatuses;
	}

	public boolean removeNextStatus(Status... statuses) {
		if (null != statuses) {
			for (Status status : statuses) {
				return nextStatuses.remove(status);
			}
		}
        return false;
	}


	public boolean addNextStatus(Status... statuses) {
		if (null != statuses) {
			for (Status status : statuses) {
				if (!this.equals(status)) {
					return nextStatuses.add(status);
				}
			}
		}
        return false;
	}
	
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((getName() == null) ? 0 : getName().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final StatusImpl other = (StatusImpl) obj;
		if (getName() == null) {
			if (other.getName() != null)
				return false;
		} else if (!getName().toLowerCase().trim().equals(other.getName().toLowerCase().trim()))
			return false;
		return true;
	}

	public int compareTo(Status obj) {
		if (this.equals(obj)) {
			return 0;
		}
		if (getClass() == obj.getClass()){
			StatusImpl other = (StatusImpl)obj;
			if (other.getNextStatuses().contains(this)) {
				return 1;
			}
			if (getNextStatuses().contains(obj)) {
				return -1;
			}
			if (getNextStatuses().isEmpty() && !other.getNextStatuses().isEmpty()) {
				return 1;
			}
			if (other.getNextStatuses().isEmpty() && !getNextStatuses().isEmpty()) {
				return -1;
			}
		}
		return 0;
	}
	
}
